package com.tiancheng.trade.settlement.controller.admin;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.exception.BusinessException;
import com.tiancheng.trade.settlement.model.settlement.StatementAccount;
import com.tiancheng.trade.settlement.service.settlement.IStatementAccountService;
import com.tiancheng.trade.settlement.vo.settlement.admin.StatementAccountCreateVO;
import com.tiancheng.trade.settlement.vo.settlement.admin.StatementAccountQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 对账函
 *
 * @author lzh
 */
@RestController
@RequestMapping("/admin/statement")
@Validated
@Slf4j
public class StatementAccountController {

    @Autowired
    IStatementAccountService statementAccountService;

    /**
     * 按id查询对账函信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Long id) throws BusinessException {
        Assert.isTrue(id != null, "参数错误~");
        return Result.success(statementAccountService.findById(id));
    }

    /**
     * 查询对账函列表信息
     *
     * @param accountQueryVO
     * @return
     */
    @GetMapping("/findPageList")
    public Result findPageList(StatementAccountQueryVO accountQueryVO) throws BusinessException {
        PageInfo<StatementAccount> result = statementAccountService.getStatementAccountPage(accountQueryVO);
        return Result.success(result);
    }

    /**
     * 新增对账函信息
     *
     * @param statementAccount
     * @return
     */
    @PostMapping("/")
    public Result add(@RequestBody StatementAccountCreateVO statementAccount) throws BusinessException {
        int data = statementAccountService.addStatementAccount(statementAccount);
        return Result.success("操作成功");
    }


    /**
     * 按对账函编号删除对账函信息
     *
     * @param queryVO
     * @return
     */
    @PostMapping("/remove")
    public Result remove(@RequestBody StatementAccountQueryVO queryVO) throws BusinessException {
        List<String> codeList = queryVO.getStatementCodeList();
        Assert.isTrue(codeList != null, "参数错误~");
        int ret = statementAccountService.deleteStatementAccount(codeList);
        if (ret == 0) {
            return Result.fail("删除失败");
        }
        return Result.success("删除成功");
    }

    /**
     * 按对账函编号查询对账函信息
     *
     * @param statementCode
     * @return
     */
    @GetMapping("/findByStatementCode")
    public Result findByStatementCode(String statementCode) throws BusinessException {
        Assert.isTrue(statementCode != null, "参数错误~");
        return Result.success(statementAccountService.findByStatementCode(statementCode));
    }

    /**
     * 对账函生成PDF文件
     *
     * @param statementCode
     * @return
     */
    @GetMapping("/statementAccountPdf")
    public Result statementAccountPdf(String statementCode, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Assert.isTrue(statementCode != null, "参数错误~");
        statementAccountService.statementAccountPdf(statementCode, request, response);
        return Result.success("导出成功");
    }

}
